package com.chenchao.designer.decorator;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/18 20:12
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javadesigner.decorator.Test1.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 装饰器模式
 */
public class Test1 {
    public static void main(String[] args) {
        Cup cup = new Cup();
        Drinkable drinkable1=new Milke(cup);
        System.out.println(drinkable1.drink());
        Drinkable drinkable2=new Milke(drinkable1);
        System.out.println(drinkable2.drink());

    }
}

/**
 * 抽象组件（需要装饰的抽象父类与接口）
 */
interface Drinkable{
    String drink();
}

/**
 * 具体组件：需要装饰的对象
 */
class Cup implements Drinkable{
    @Override
    public String drink() {
        return "一杯";
    }
}

/**
 * 抽象装饰类:包含了对抽象组件的引用以及装饰着共有的方法
 * 抽象类的目的是实现更多装饰可能
 */
abstract class coffee implements Drinkable{
    Drinkable cup;
    //包含了对要装饰组件的引用
    public coffee(Drinkable cup) {
        this.cup = cup;
    }

    @Override

    public String drink() {
        return cup.drink()+"coffee";
    }
}

/**
 * 装饰类
 */
class Milke extends coffee{
    public Milke(Drinkable cup) {
        super(cup);
    }

    @Override

    public String drink() {
        return super.drink()+"加了奶";
    }
}
class Sugar extends coffee {
    public Sugar(Drinkable cup) {
        super(cup);
    }

    @Override

    public String drink() {
        return super.drink()+"加了糖";
    }
}